#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

#define START 100
#define END   30000
static int is_primer (int n);

int main (void)
{
	pid_t pid;
	for (int i = START; i <= END; i++) {
		pid = fork ();
		if (-1 == pid) {
			perror ("fork()");
			return -1;
		}
		if (0 == pid) {
			//子进程 负责筛选质数
			if (is_primer (i)) {
				printf ("%d is a primer!\n", i);
			}
			exit (0);
		} 
	}
	for (int i = START; i <= END; i++) {
		wait (NULL);
	}
	return 0;
}
/*筛选质数：返回1表示是一个质数*/
static int is_primer (int n) 
{
	int i;
	if (n < 2) {
		return 0;
	}
	for (i = 2; i < n; i++) {
		if ((n % i) == 0) {
			return 0;
		}
	}
	return 1;
}




